<template>
  <div>
    <el-dialog
      title="验证身份"
      :visible.sync="passDialogData.isShow"
      width="480px"
      top="35vh"
      @close="closeDialog"
    >
      <el-form
        ref="passFormRef"
        :rules="ruleForm"
        :model="passForm"
        label-width="50px"
        class="form-box"
      >
        <el-form-item prop="phone">
          <el-input
            v-model="passForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="passForm.code"
            placeholder="请输入短信验证码"
            class="btn-input"
          ></el-input>
          <el-button type="warning" size="small" class="code-btn">
            获取验证码
          </el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" plain @click="passDialogData.isShow = false"
          >取 消</el-button
        >
        <el-button type="success" class="submitPass" @click="comfirmBtn"
          >确 认</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ['passDialogData'],
  data() {
    // 手机号自定义验证规则
    let checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
        console.log(reg.test(value))
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    return {
      // 审核通过表单
      passForm: {
        phone: '', // 手机号
        code: '' // 验证码
      },
      // 表单验证规则
      ruleForm: {
        phone: [{ validator: checkPhone, trigger: 'blur' }],
        code: [{ required: true, message: '验证码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 对话框关闭
    closeDialog() {
      this.$refs.passFormRef.resetFields()
      this.passForm.phone = ''
      this.passForm.code = ''
    },
    // 对话框确定按钮
    comfirmBtn() {
      // 表单预验证
      this.$refs.passFormRef.validate((valid) => {
        if (!valid) {
          return false
        }
        this.passDialogData.isShow = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.submitPass {
  background-color: #1dc8a4;
}
.dialogForm {
  margin-bottom: 50px;
}
.btn-input {
  width: 180px !important;
  margin-right: 10px;
}
.code-btn {
  height: 30px;
}
</style>
